<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/static/lib/layui-v2.8.4/css/layui.css}" media="all">
</head>
<body style="padding: 30px">
<div class="layui-form">
    <div class="layui-row layui-col-space12">
        <div class="layui-col-md4 layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-user"></i>
                </div>
                <input type="text" lay-verify="required|username" lay-reqtext="用户名不能为空" placeholder="用户账户"
                       th:value="${user.username}" class="layui-input layui-disabled" disabled>
            </div>
        </div>
        <div class="layui-col-md4 layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-username"></i>
                </div>
                <input type="text" lay-verify="required|name"  name="name"  lay-reqtext="用户姓名不能为空" placeholder="用户姓名"
                       th:value="${user.name}" class="layui-input">
            </div>
        </div>
        <div class="layui-col-md4 layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i class="layui-icon layui-icon-email"></i>
                </div>
                <input type="text" lay-verify="required|email" name="email" lay-reqtext="用户邮箱不能为空" placeholder="邮箱"
                       th:value="${user.email}" class="layui-input">
            </div>
        </div>
        <div class="layui-col-md4 layui-form-item">
            <div class="layui-input-wrap">
                <div class="layui-input-prefix">
                    <i>☏</i>
                </div>
                <input type="text" lay-verify="required|phone" name="phone" lay-reqtext="用户手机号不能为空" placeholder="手机号"
                       th:value="${user.phone}" class="layui-input">
            </div>
        </div>
    </div>
    <input type="hidden" name="id" placeholder="用户ID" th:value="${user.id}" class="layui-input" >
    <div class="layui-row">
        <div class="layui-col-md12" style="text-align: center;">
            <button class="layui-btn layui-btn-primary" lay-filter="saveBtn" lay-submit>保存</button>
        </div>
    </div>
</div>
<script>
    let ctx = '[[${#servletContext.contextPath}]]';
</script>
<script th:src="@{/static/lib/layui-v2.8.4/layui.js}" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        let form = layui.form;
        // 自定义验证规则
        form.verify({
            //     用户姓名不能是字符和数字，只能是英文或中文
            name: function (value) {
                if (!/^[\u4e00-\u9fa5a-zA-Z]+$/.test(value)) {
                    return '用户姓名不能是字符和数字，只能是英文或中文';
                }
            },
            //     电话号码必须是数字
            phone: function (value) {
                if (!/^[0-9]+$/.test(value)) {
                    return '电话号码必须是数字';
                }
            },
            // email 必须符合是这个 规范/^\d{6,10}@[a-zA-Z]{2,7}\.[a-zA-Z]{2,7}$/，如果不正确，离开输入框时会提示：邮箱格式不正确
            email: function (value) {
                if (!/^\d{6,10}@[a-zA-Z]{2,7}\.[a-zA-Z]{2,7}$/.test(value)) {
                    return '邮箱格式不正确';
                }
            }
        });


        //监听提交
        form.on('submit(saveBtn)', function (data) {
            data = data.field;
            console.log(data);
            // 发送请求
            fetch(ctx + '/sysUser/edit', {
                method: 'post',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(data)
            }).then(function (response) {
                return response.json();
            }).then(function (data) {
                if (data.code == 200) {
                    alert("保存成功");
// 关闭当前页面
                    window.close();
// 刷新父页面
                    parent.location.reload();
                } else {
                    alert("保存失败");
                }
            })
        });
        return false;
    });
</script>
</body>
</html>